package cglgu.trellis.chado.das2.modelimpl.cglgu;

/*
 * $Id: ChadoSegmentsCapability.java 191 2011-03-27 00:38:22Z sundaram.georgetown@gmail.com $
 * $Author: sundaram.georgetown@gmail.com $
 * $Date: 2011-03-27 00:38:22 +0000 (Sun, 27 Mar 2011) $
 * $Rev: 191 $
 * $HeadURL: http://genomancer.googlecode.com/svn/branches/gu-dev-branch/tengcha/src/cglgu/trellis/chado/das2/modelimpl/cglgu/ChadoSegmentsCapability.java $
 */

import genomancer.trellis.das2.model.Das2CoordinatesI;
import genomancer.trellis.das2.model.Das2LocationI;
import genomancer.trellis.das2.model.Das2SegmentI;
import genomancer.trellis.das2.model.Das2SegmentsCapabilityI;
import genomancer.trellis.das2.model.Das2SegmentsResponseI;
import genomancer.vine.das2.client.modelimpl.Das2Coordinates;
import genomancer.vine.das2.client.modelimpl.Das2GenericCapability;
import genomancer.vine.das2.client.modelimpl.Das2SegmentsResponse;
import genomancer.vine.das2.client.modelimpl.Das2Version;
import cglgu.trellis.chado.das2.modelimpl.ChadoSegmentsCapability;
import cglgu.trellis.chado.das2.cglgu.db.CglguDbUtil;
import java.net.URI;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;


public class ChadoSegmentsCapability extends Das2GenericCapability implements Das2SegmentsCapabilityI  {
	static protected int SEQ_COUNT_THRESHOLD = ChadoSourcesCapability.max_seqs_allowed;
	Das2SegmentsResponseI segments_response = null;
	ChadoVersion chado_version = null;
	Map<URI,Das2SegmentI> uri2segment = null;
	boolean initialized = false;
	int seq_count = 0;
	CglguDbUtil dbutil = null;

	protected synchronized boolean initSegments()  {
		boolean success = false;
		uri2segment = new HashMap<URI,Das2SegmentI>();
		List<Das2SegmentI> segments = new ArrayList<Das2SegmentI>();
		URI seq_base_uri = this.getAbsoluteURI().resolve("./");

		seq_count = dbutil.getChromosomeCount();	

		System.out.println("Number of chromosomes in feature table: " + seq_count);
		if (seq_count > SEQ_COUNT_THRESHOLD)  {
			System.out.println("WARNING: large number of sequences for " + version.getLocalURIString()
					+ ", count = " + seq_count);
			// alternative to loading all seqs -- 
			//   maybe set to respond with TOO_LARGE status, and rather than loading all seqs 
			//   do conditional in getSegment() to just retrieve desired??
			// more drastic alternative -- move seq_count check to ChadoSourcesCapability and 
			//    if too large then don't list a "segments" capability?
		}

		ResultSet rs = dbutil.getChromosomeListRS();

		while (rs.next()){
			ChadoSequence seq = new ChadoSequence(seq_base_uri, chadoVersion, rs);
			segments.add(seq);
		}

		segments_response = new Das2SegmentsResponse(seq_base_uri, segments, null, null);
		for (Das2SegmentI seg : segments)  {
			// for partial genomes with lots of contigs (>10K, sometimes >200K) 
			//    creating this hash requires _lots_ of memory overhead
			//    (actually its the call to seg.getAbsoluteURI(), which triggers 
			//    unique URI creation (plus several strings?))
			uri2segment.put(seg.getAbsoluteURI(), seg);
		}
		success = true;
		initialized = true;
	}

	return success;
}